//
//  Home.swift
//  ChinaLandMark
//
//  Created by Felix Hi on 2021/6/29.
//

import SwiftUI

struct Home: View {
    @State private var isProfilePresented = false
    
    var categories: [String: [Landmark]] {
        Dictionary(grouping: landmarks) { $0.category }
    }
    
    
    var body: some View {
        NavigationView {
            List {
                /**
                Image("testimg1")
                    .resizable()
                    .scaledToFill()
                    .frame(height: 200)
                    .clipped()
                    .listRowInsets(EdgeInsets())
                 */
                
                PageVC()
                    .frame(height: 200)
                    .listRowInsets(EdgeInsets())
                    
                
                ForEach (categories.keys.sorted(), id: \.self) { categoryName in
                    CategoryCell(categoryName: categoryName, landmarks: self.categories[categoryName]!)
                }
                .listRowInsets(EdgeInsets())
                
                NavigationLink(destination: LandMarkList()) {
                    Text("查看所有地标")
                }
            }
            .navigationBarTitle(Text("精选"))
            .navigationBarItems(trailing:
                Button(action: {
                    self.isProfilePresented = true
                }) {
                    Image(systemName: "person.crop.circle")
                        .imageScale(.large)
                        .accessibility(label: Text("User Profile"))
                        .padding()
                }
            ).sheet(isPresented: $isProfilePresented) {
                SecondScreen(dismissFlag: self.$isProfilePresented)
            }
        }
    }
}

struct Home_Previews: PreviewProvider {
    static var previews: some View {
        Home()
    }
}

struct SecondScreen: View {
    @Environment(\.editMode) var mode
    @Binding var dismissFlag: Bool
    
    @State private var profile = User.default
    @State private var profileCopy = User.default
    
    var birthday: String {
       let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy年M月dd日"
        return dateFormatter.string(from: profile.goalDate)
    }
    
    var body: some View {
        VStack (spacing: 20) {
            HStack {
                if mode?.wrappedValue == .active {
                    Button(action: {
                        profile = profileCopy
                        mode?.animation().wrappedValue = .inactive
                    }) {
                        Text("完成")
                    }
                }
                    
                Spacer()
                
                EditButton().padding()
            }
            
            if self.mode?.wrappedValue == .inactive {
                List {
                    Button(action: {
                        self.dismissFlag = false
                    }) {
                        Text("Second screen, click to exit")
                    }
                    
                    Text(profile.username)
                        .font(.title)
                        .bold()
                    
                    Text("允许通知: \(profile.prefersNotifications ? "是" : "否")")
                    
                    Text("喜欢的季节: \(profile.seasonalPhoto.rawValue)")
                    
                    Text("生日: \(birthday)")
                    
                    VStack (alignment: .leading) {
                        Text("最近旅行的徒步").font(.headline)
                        HikeView(hike: hikeData[0])
                    }
                }
            } else {
                List {
                    HStack {
                        Text("昵称")
                        Divider()
                        TextField("昵称", text: $profileCopy.username)
                    }
                    
                    Toggle(isOn: $profileCopy.prefersNotifications) {
                        Text("允许通知")
                    }
                    
                    VStack (alignment: .leading) {
                        Text("喜欢的季节")
                        Picker("", selection: $profileCopy.seasonalPhoto) {
                            ForEach(User.Season.allCases, id: \.self) { season in
                                Text(season.rawValue).tag(season)
                            }
                        }.pickerStyle(SegmentedPickerStyle())
                    }
                    .padding(.top)
                    
                    HStack(alignment: .top) {
                        Text("生日").bold()
                        DatePicker(
                                "",
                                selection: $profileCopy.goalDate,
                                in: Calendar.current.date(byAdding: .year, value: -1,
                                to: profile.goalDate)! ... Calendar.current.date(byAdding: .year, value: 1, to: profile.goalDate)!,
                            displayedComponents: .date
                        )
                    }
                    .padding(.top)
                }
                .onDisappear {
                    profileCopy = profile
                }
            }
        }
        .padding()
    }
}

